package com.cloudpivot.receivales.ding.scenegroup.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.service.DingBasicService;
import com.cloudpivot.receivales.ding.basic.service.impl.HttpAPIServiceForDing;
import com.cloudpivot.receivales.ding.scenegroup.domain.CoolAppActionMsgDTO;
import com.cloudpivot.receivales.ding.scenegroup.domain.CoolAppDTO;
import com.cloudpivot.receivales.ding.scenegroup.service.CoolAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CoolAppServiceImpl implements CoolAppService {


    @Autowired
    private HttpAPIServiceForDing httpAPIServiceForDing;

    @Autowired
    private DingBasicService dingBasicService;


    /**
     * 群聊推送
     * @param dto
     * @return
     */
    @Override
    public String pushCoolappMsgOnGroup(CoolAppActionMsgDTO dto) {
        CoolAppDTO mainDto = new CoolAppDTO();
        mainDto.setMsgParam(JSONObject.toJSONString(dto));
        //配置机器人需要推送的标准卡片信息
        mainDto.setCoolAppCode(dto.getCoolAppId());

        mainDto.setMsgKey("sampleActionCard");
        mainDto.setOpenConversationId(dto.getOpenConversationId());
        mainDto.setRobotCode("ding7g1emlbhlfc6mqlx");
        String ret = requestDingService(mainDto);
        return ret;
    }

    /**
     * 单聊推送
     * @param dto
     * @return
     */
    @Override
    public String pushCoolappMsgOnSingle(CoolAppActionMsgDTO dto) {
        CoolAppDTO mainDto = new CoolAppDTO();
        mainDto.setMsgParam(JSONObject.toJSONString(dto));
        //配置机器人需要推送的标准卡片信息
        mainDto.setCoolAppCode(dto.getCoolAppId());

        mainDto.setMsgKey("sampleActionCard");
        mainDto.setOpenConversationId(dto.getOpenConversationId());
        mainDto.setRobotCode("ding7g1emlbhlfc6mqlx");
        String ret = requestDingServiceSingle(mainDto);
        return ret;
    }

    /**
     * 推送 首次酷应用创建成功提醒 单聊推送
     *群内暂时不提醒
     * @param dto
     * @return
     */
    @Override
    public String pushHelloCoolappMsg(CoolAppActionMsgDTO dto) {
        CoolAppDTO mainDto = new CoolAppDTO();
        //使用默认通知
        JSONObject _msg = new JSONObject();
        _msg.put("content", "\"报价账本\"酷应用安装成功,聊天窗口左下角-点击\"报价账本\"进入酷应用(如果没有显示,请切换下聊天窗口!)");
        mainDto.setMsgParam(_msg.toJSONString());
        //配置机器人需要推送的标准卡片信息
        mainDto.setCoolAppCode(dto.getCoolAppId());
        mainDto.setMsgKey("sampleText");
        mainDto.setOpenConversationId(dto.getOpenConversationId());
        mainDto.setRobotCode("ding7g1emlbhlfc6mqlx");
        String ret = requestDingServiceSingle(mainDto);
        return ret;
    }

    private String requestDingService(CoolAppDTO dto) {
        String access_token = dingBasicService.getToken("coolapp");
        String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
        try {
            log.info("打印 请求前的对象====>{}", JSONObject.toJSONString(dto));
            String ret = httpAPIServiceForDing.doPostDing(url, JSONObject.toJSONString(dto), access_token);
            log.info("创建创建酷应用消息====>>>>>>返回值：{}", ret);
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private String requestDingServiceSingle(CoolAppDTO dto) {
        String access_token = dingBasicService.getToken("coolapp");
        String url = "https://api.dingtalk.com/v1.0/robot/privateChatMessages/send";
        try {
            log.info("打印 请求前的对象====>{}", JSONObject.toJSONString(dto));
            String ret = httpAPIServiceForDing.doPostDing(url, JSONObject.toJSONString(dto), access_token);
            log.info("创建创建酷应用消息====>>>>>>返回值：{}", ret);
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
